/**
 * Created by Administrator on 2016/6/24 0024.
 */
$().ready(function () {
    $(".opera .bar").on('tap', function () {
        $(".opera .bar").removeClass('active');
        $(this).addClass('active');
        var target = $(this).index();
        $('.LeftBar-bottom div').css('display', 'none');
        $('.LeftBar-bottom div').eq(target).css('display', 'block');
    })
    //点击饮品类
    $(".opera-item1 li").on('tap', function () {
        window.location.href = MPJ.base+'/pages/product/product-list.jsp';
    })

    //点击饮品类
    $(".opera-item2 li").on('tap', function () {
        window.location.href =  MPJ.base+'/pages/product/product-list.jsp';
    })

    $('#online-shop').on('tap',function(){
        window.location.href =  MPJ.base+'/pages/product/product-list.jsp';
    })

    $('#weekly-special').on('tap', function () {
        window.location.href =  MPJ.base+'/pages/product/weekly-specials.jsp';
    })
    //预定
    $('#pre-orders').on('tap', function () {
        window.location.href =  MPJ.base+'/pages/pre-order.jsp';
    })
    $('#features').on('tap', function () {
        window.location.href =  MPJ.base+'/pages/other/features.jsp';
    })
    $('#popular-brands').on('tap', function () {
        window.location.href =  MPJ.base+'/pages/popular-brand.jsp';
    })
    $('#popular-category').on('tap', function () {
        window.location.href =  MPJ.base+'/pages/popular-category.jsp';
    })
    //点击wine corner
    $('#wine-corner').on('tap', function () {
        sessionStorage.removeItem('wine_index');
        window.location.href =  MPJ.base+'/pages/wine/wine-corner.jsp';
    })
    $('#hampers').on('tap', function () {
        window.location.href =  MPJ.base+'/pages/hampers.jsp';
    })
    $('#stamp-collection').on('tap', function () {
        window.location.href =  MPJ.base+'/pages/stamp-collection.jsp';
    })
    //点击饮品类
    $("#drink").on('tap', function () {
        window.location.href =  MPJ.base+'/pages/drinks-list.jsp';
    })
    //店铺定位
    $('#octopusReward').on("tap", function () {
        window.location.href = MPJ.base+'/pages/octopus-reward.jsp';
    })
    //店铺定位
    $('#store-locator').on("tap", function () {
        window.location.href =  MPJ.base+'/pages/store/store-location.jsp';
    })
    //refer a friend
    $('#refer-friend').on("tap", function () {
        window.location.href =  MPJ.base+'/pages/other/referfriend-main.jsp';
    })

    $('#about').on("tap", function () {
        window.location.href =  MPJ.base+'/pages/other/about.jsp';
    });

    loadMenuIsSuccess($('.opera-item2 ul'));

    $('.opera-item2').on('tap', 'li', function (index) {
        var id = $(this).data('id');
        var name = $(this).html();
        var url= $(this).data('url');

        sessionStorage.removeItem('category_Arr');
        var category_Arr = [];

        var dpm_classify = {};
        dpm_classify.id = id;
        dpm_classify.name = name;
        dpm_classify.url = url;

        category_Arr.push(dpm_classify);
        sessionStorage.setItem('category_Arr', JSON.stringify(category_Arr));
        location.href =  MPJ.base+"/pages/product/product-list.jsp";
    })
})


//分类菜单是否加载成功
function loadMenuIsSuccess(obj) {
    var classify_Arr = JSON.parse(sessionStorage.getItem('classify_Arr'));
    if (classify_Arr != undefined && classify_Arr != "" && classify_Arr != null) {
        var department_str = '';
        for (var i = 0; i < classify_Arr.length; i++) {
            department_str += '<li class="mui-table-view-cell" data-url="'+ classify_Arr[i].department_url+'" data-id="' + classify_Arr[i].department_id + '">' + classify_Arr[i].department_name + '</li>';
        }
        obj.html(department_str);

    } else {
        loadHideMenu();
    }
}

//获取分类菜单
function loadHideMenu() {
    $.ajax({
        type: "get",
        url: MPJ.apiBase+'/mpj2shop/docroot/en/das.xml',
        dataType: "xml",
        async: false,
        success: function (obj) {
            var classify_Arr = [];
            $(obj).find('Department').each(function (i) {
                var department_Arr = {};
                department_Arr.department_id = $(this).attr('id');
                department_Arr.department_name = $(this).children('Name').text();
                var icon_url='';
                if(i==0){
                    icon_url=MPJ.base+'/resources/images/icon/icon_fresh-food.gif';
                }else if(i==1){
                    icon_url=MPJ.base+'/resources/images/icon/icon_chilled.gif';
                }else if(i==2){
                    icon_url=MPJ.base+'/resources/images/icon/icon_grocery.gif';
                }else if(i==3){
                    icon_url=MPJ.base+'/resources/images/icon/icon_drinks.gif';
                }else if(i==4){
                    icon_url=MPJ.base+'/resources/images/icon/icon_beer.gif';
                }else if(i==5){
                    icon_url=MPJ.base+'/resources/images/icon/icon_health.gif';
                }else if(i==6){
                    icon_url=MPJ.base+'/resources/images/icon/icon_household.gif';
                }else if(i==7){
                    icon_url=MPJ.base+'/resources/images/icon/icon_homeware.gif';
                }else if(i==8){
                    icon_url=MPJ.base+'/resources/images/icon/icon_baby.gif';
                }else if(i==9){
                    icon_url=MPJ.base+'/resources/images/icon/icon_pets.gif';
                }
                department_Arr.department_url=icon_url;
                $department = $(obj).find('Department').eq(i);
                var aisle = [];
                $department.find('Aisle ').each(function (j) {
                    var aisle_Arr_next = {};
                    aisle_Arr_next.aisle_id = $(this).attr('id');
                    aisle_Arr_next.aisle_name = $(this).children('Name').text();
                    aisle.push(aisle_Arr_next);

                    var shelf = [];
                    $(this).find('Shelf ').each(function (k) {
                        var shelf_Arr_next = {};
                        shelf_Arr_next.shelf_id = $(this).attr('id');
                        shelf_Arr_next.shelf_name = $(this).children('Name').text();
                        shelf.push(shelf_Arr_next);
                    })
                    aisle_Arr_next.shelf = shelf;
                })
                department_Arr.aisle = aisle;
                classify_Arr.push(department_Arr);
            })
            sessionStorage.setItem('classify_Arr', JSON.stringify(classify_Arr));
            console.log(classify_Arr);
            loadMenuIsSuccess($('.opera-item2 ul'));
        },
        error: function () {

        }
    });
}
